package com.yatop.lalmbda.ai.model;


import com.yatop.lalmbda.ai.service.AccountService;
import com.yatop.lalmbda.ai.service.TaskService;
import com.yuyaogc.lowcode.engine.annotation.*;
import com.yuyaogc.lowcode.engine.context.Context;
import com.yuyaogc.lowcode.engine.plugin.activerecord.Model;
import com.yuyaogc.lowcode.engine.util.enums.DataTypeEnum;

import java.math.BigDecimal;
import java.sql.Timestamp;

@Table(displayName = "任务", name = "ai_task")
public class Task extends Model<Task> {

    public Task(){}


    @Id
    private Long id;

    @Column(label = "用户id")
    private Long userId;

    @Column(label = "任务名称",name = "task_name")
    @NotBlank
    private String taskName;

    @Column(label = "任务内容", type = DataTypeEnum.TEXT, name = "task_context")
    private String taskContext;

    @ManyToOne(displayName = "模型")
    @JoinColumn(name = "model_id")
    private AiModel model;


    @Column(label = "任务id", name = "task_id")
    private String taskId;

    @Column(label = "请求id", name = "request_id")
    private String requestId;

    @Selection(options = {
            @Option(label = "图片", value = "jpg"),
            @Option(label = "文本", value = "text")
    })
    private String taskType;

    @Column(label = "任务开始时间")
    private Timestamp taskBeginTime;

    @Column(label = "任务结束时间")
    private Timestamp taskEndTime;

    @Column(label = "任务状态")
    @Selection(options = {
            @Option(label = "待处理", value = "PENDING"),
            @Option(label = "进行中", value = "IN_PROGRESS"),
            @Option(label = "已完成", value = "COMPLETED"),
            @Option(label = "已取消", value = "CANCELLED"),
            @Option(label = "失败", value = "FAILED"),
            @Option(label = "超时", value = "TIMEOUT"),
            @Option(label = "成功", value = "SUCCEEDED")
    })
    private String taskStatus;

    @Column(label = "优先级")
    @Selection(options = {
            @Option(label = "高", value = "HIGH"),
            @Option(label = "中", value = "MEDIUM"),
            @Option(label = "低", value = "LOW")
    })
    private String priority;

    @Column(label = "创建时间",name = "create_time")
    private Timestamp createTime;

    @Column(label = "更新时间", name = "update_time")
    private Timestamp updateTime;

    @Autowired
    private TaskService taskService;
    @Autowired
    private AccountService accountService;



    @Route(displayName = "创建")
    public boolean create(Task value){

        BigDecimal money = accountService.accountBalance(Context.getInstance().getUserId());
        if (money.compareTo(BigDecimal.ZERO)<0) {
            // 处理金额小于 0 的情况，例如抛出异常或记录日志
            throw new IllegalArgumentException("账户余额不能为负数！");
        }

       taskService.create(value);


        return false;
    }

    public Long getId() {
        return (Long) this.get("id");
    }

    public Task setId(Long id) {
        this.set("id", id);
        return this;
    }

    public Long getUserId() {
        return (Long) this.get("userId");
    }

    public Task setUserId(Long userId) {
        this.set("userId", userId);
        return this;
    }

    public String getTaskName() {
        return (String) this.get("taskName");
    }

    public Task setTaskName(String taskName) {
        this.set("taskName", taskName);
        return this;
    }

    public String getTaskContext() {
        return (String) this.get("taskContext");
    }

    public Task setTaskContext(String taskContext) {
        this.set("taskContext", taskContext);
        return this;
    }



    public String getTaskId() {
        return (String) this.get("taskId");
    }

    public Task setTaskId(String taskId) {
        this.set("taskId", taskId);
        return this;
    }

    public String getRequestId() {
        return (String) this.get("requestId");
    }

    public Task setRequestId(String requestId) {
        this.set("requestId", requestId);
        return this;
    }

    public String getTaskType() {
        return (String) this.get("taskType");
    }

    public Task setTaskType(String taskType) {
        this.set("taskType", taskType);
        return this;
    }

    public Timestamp getTaskBeginTime() {
        return (Timestamp) this.get("taskBeginTime");
    }

    public Task setTaskBeginTime(Timestamp taskBeginTime) {
        this.set("taskBeginTime", taskBeginTime);
        return this;
    }

    public Timestamp getTaskEndTime() {
        return (Timestamp) this.get("taskEndTime");
    }

    public Task setTaskEndTime(Timestamp taskEndTime) {
        this.set("taskEndTime", taskEndTime);
        return this;
    }

    public String getTaskStatus() {
        return (String) this.get("taskStatus");
    }

    public Task setTaskStatus(String taskStatus) {
        this.set("taskStatus", taskStatus);
        return this;
    }

    public String getPriority() {
        return (String) this.get("priority");
    }

    public Task setPriority(String priority) {
        this.set("priority", priority);
        return this;
    }

    public Timestamp getCreateTime() {
        return (Timestamp) this.get("createTime");
    }

    public Task setCreateTime(Timestamp createTime) {
        this.set("createTime", createTime);
        return this;
    }

    public Timestamp getUpdateTime() {
        return (Timestamp) this.get("updateTime");
    }

    public Task setUpdateTime(Timestamp updateTime) {
        this.set("updateTime", updateTime);
        return this;
    }
}
